package com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.bean.LoginInfo;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.ImagesBean;
import com.tsinghuabigdata.edu.ddmath.util.AccountUtils;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;
import com.tsinghuabigdata.edu.ddmath.util.GlobalData;
import com.tsinghuabigdata.edu.ddmath.util.PicassoUtil;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;


/**
 * Created by fish on 2016/10/20.
 */

public class DayQuestionsAdapter extends RecyclerView.Adapter {

    private static final String TAG = "SelectQuestionsActivity";

    private Context                   mContext;
    private List<ImagesBean>          mList;
    private HashMap<Integer, Boolean> mHashMap;
    private QuestionSelectListener    mQuestionSelectListener;

    private String accessToken;


    public DayQuestionsAdapter(Context context, List<ImagesBean> data) {
        mContext = context;
        mList = data;
        LoginInfo loginInfo = AccountUtils.getLoginUser();
        if (loginInfo != null) {
            try {
                accessToken = URLEncoder.encode(loginInfo.getAccessToken(), "utf-8");
            } catch (Exception e) {
                AppLog.d(e.toString());
            }
        }
    }

    public void setQuestionSelectListener(QuestionSelectListener questionSelectListener) {
        mQuestionSelectListener = questionSelectListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(mContext).inflate(R.layout.item_day_questions, parent, false);
        return new RankViewHolder(root);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        RankViewHolder holder1 = (RankViewHolder) holder;
        final ImagesBean vo = mList.get(position);

        String comUrl = vo.getPath();// + "?access_token=" + accessToken;
//        Log.i(TAG, "comUrl=" + comUrl);
        PicassoUtil.getPicasso(mContext).load(comUrl).error(R.drawable.ic_broken_image).placeholder(R.drawable.ic_temporary_image).into(holder1.iv);

        if (mHashMap.containsKey(position)) {
            holder1.cb.setChecked(true);
        } else {
            holder1.cb.setChecked(false);
        }
        holder1.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                if (checkBox.isChecked()) {
                    mHashMap.put(position, true);
                    GlobalData.getTeamMap().put(vo.getImageId(), true);
                } else {
                    mHashMap.remove(position);
                    GlobalData.getTeamMap().remove(vo.getImageId());
                }
                Log.i(TAG, "DayQuestionsAdapter QustionMap = " + GlobalData.getQustionMap().keySet().size());
                Log.i(TAG, "DayQuestionsAdapter TeamMap = " + GlobalData.getTeamMap().keySet().size());
                monitorSize();
            }
        });
    }

    private void monitorSize() {
        int size = mHashMap.keySet().size();
        Log.i("sky", "monitorSize=" + size + " mList.size()=" + mList.size());
        if (size < mList.size()) {
            mQuestionSelectListener.select("1", false);
        } else {
            mQuestionSelectListener.select("1", true);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<ImagesBean> list) {
        mList.clear();
        mList.addAll(list);
        //        Log.i("sky", "list.size()=" + list.size());
        //        Log.i("sky", "mList.size()=" + mList.size());
    }


    public void setMap(HashMap<Integer, Boolean> hashMap) {
        mHashMap = hashMap;
        //        for (int i = 0; i < mList.size(); i++) {
        //            if (mHashMap.get(i) == null) {
        //                mHashMap.put(i, false);
        //            }
        //        }
    }

    class RankViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        CheckBox  cb;

        public RankViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_question);
            cb = (CheckBox) itemView.findViewById(R.id.cb_select_questions);
        }
    }

    public interface QuestionSelectListener {
        void select(String questionId, boolean allSelect);
    }

}
